library("pheatmap")
library("colorRamps")
library("ComplexHeatmap")
library("vegan")
library("circlize")
cluser = read.csv(file = 'ConsensusClusterResults.k=3.consensusClass.csv', sep = ',', header = F)
rt <- read.csv(file = 'gsva_go_matrix.csv', sep = ',', header = T, row.names = 1)
rownames(cluser) <- cluser[,1]
names(cluser)[2] <- "Cluster"
cluster1_rows <- rownames(cluser[cluser$Cluster == "1", ])#看一下1有多少，这里是221
rt_intersect1 <- rt[, intersect(colnames(rt), cluster1_rows)]
##再来一遍
cluster2_rows <- rownames(cluser[cluser$Cluster == "2", ])#看一下2有多少，这里是276
rt_intersect2 <- rt[, intersect(colnames(rt), cluster2_rows)]
cluster3_rows <- rownames(cluser[cluser$Cluster == "3", ])#看一下3有多少，这里是176
rt_intersect3 <- rt[, intersect(colnames(rt), cluster3_rows)]

merged <- cbind(rt_intersect1, rt_intersect2, rt_intersect3)
merged<-as.matrix(merged)
merged <- decostand(merged,"standardize",MARGIN = 1)#标准化
col_fun = colorRamp2(c(-2, 0, 2), c("#1f60a4","white","#a40d20"))#,space = "RGB"
col_Cluster1 <- c("1" = "#B49FDA", "2" = "#72AFD3", "3" = "#5FDFFF")
cluser <- cluser[colnames(merged),]
column_ann <- HeatmapAnnotation(Cluster=cluser$Cluster,
                                col = list(Cluster = col_Cluster1)
)

draw(column_ann)
p1 <- Heatmap(merged,
              col = col_fun,
              cluster_rows = T,##打开行聚类
              cluster_columns = T,##关闭列聚类##show_row_dend = T, show_column_dend = F聚类但是不显示聚类树
              show_row_names = T,##行名
              show_column_names = T, ##列名
              #column_names_rot = 2,##列名旋转方向
              column_names_side = "top",#上方列名
              #column_dend_side = "bottom"，#同上
              ##row_km = 2, #k-mean法分割行
              ##column_split = split,##自定义分割
              top_annotation = column_ann
)

# p1
# 复杂热图
Cairo::CairoTIFF(file="p1.tiff", width=8, height=8,units="in",dpi=150)
p1
dev.off()

pdf("Heatmap2.pdf",width = 8,height = 8)
p1
dev.off()
